package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletionService;

/**
 * <p>
 * banner管理接口
 * </p>
 *
 * @author yky
 * @since 2020-07-12
 */
@RestController
@RequestMapping("/cmsservice/banner")
@CrossOrigin
public class CrmBannerController {

    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * 分页查询
     * @param page  当前页
     * @param limit 每页显示数目
     * @return
     */
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(
            @PathVariable("page")long page,
            @PathVariable("limit")long limit){
        Page<CrmBanner>crmpage = new Page<CrmBanner>(page,limit);

//        QueryWrapper<CrmBanner> weapper = new QueryWrapper<>();

        crmBannerService.page(crmpage,null);

        return R.ok().data("crmpage",crmpage.getRecords()).data("total",crmpage.getTotal());
    }

    //添加
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        if(StringUtils.isEmpty(crmBanner)) return R.error().message("对象为空，添加数据后再提交");
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //修改
    @PutMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        if(StringUtils.isEmpty(crmBanner)) return R.error().message("对象为空，添加数据后再提交");
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    //删除
    @DeleteMapping("removeBanner/{id}")
    public R removeBanner(@PathVariable("id") String id){
        if(StringUtils.isEmpty(id)){
            return R.ok().message("id为空");
        }
        boolean flag = crmBannerService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }

    //根据id查询
    @GetMapping("getBanner/{id}")
    public R getBanner(@PathVariable("id") String id){
        CrmBanner banner = crmBannerService.getById(id);

        return R.ok().data("banner",banner);
    }

}

